<?php

namespace App\Middleware;

use App\Model\Mysql\ProjectModel;
use App\Model\Redis\Redis;
use App\Model\Redis\UserRedis;
use EasySwoole\Http\Request;

class ApiMiddleware extends BaseMiddleware
{
    /**
     * 中间件入口
     * @param Request $request
     * @return bool
     */
    public function handle(Request $request)
    {
        $path = $request->getUri()->getPath();

        $middlewareGroup = $this->middlewareGroup($path);
        foreach ($middlewareGroup as $middleware) {
            $result = $this->$middleware($request);
            // 如果不是true的话，就直接返回结果
            if ($result !== true) {
                return $result;
            }
        }
        return true;
    }

    /**
     * 中间件过程
     * apiBase, apiMiniLogin
     * @param $path
     * @return array
     */
    public function middlewareGroup($path)
    {
        $middlewareGroup[] = 'apiMiniBase';
        switch ($path) {
//            case '/api/v1/index/search':
//            case '/api/v1/index/searchDelete':
//
//            case '/api/v1/project/category':
//            case '/api/v1/project/detail':

            case '/api/v1/project/userVideo':
            case '/api/v1/project/moreVideos':
            case '/api/v1/project/projectCheck':
            case '/api/v1/project/projectSubmit':
            case '/api/v1/project/examineDetail':

            case '/api/v1/order/orderRequire':
            case '/api/v1/order/orderSubmit':
            case '/api/v1/order/orderList':
            case '/api/v1/order/orderDetail':

            case '/api/v1/user/examine':
            case '/api/v1/user':
            case '/api/v1/user/invite':

                $middlewareGroup[] = 'miniLogin';
                break;
            case '/api/v1/index/renovation':
            case '/api/v1/index/label':
            case '/api/v1/project':

            case '/api/v1/index/search':
            case '/api/v1/index/searchDelete':
            case '/api/v1/project/category':
            case '/api/v1/project/detail':

                $middlewareGroup[] = 'waitLogin';
                break;

        }
        return $middlewareGroup;
    }

    /**
     * 小程序登录
     * @param Request $request
     * @return string|array|bool
     */
    private function miniLogin(Request $request)
    {
        $header = $request->getHeaders();
        $token = $header['token'][0] ?? '';
        if (!empty($token)) {
            $user = UserRedis::getInstance()->user($token);
            if (!empty($user)) {
                $request->withAttribute('user', $user);
            } else {
                return ['status' => '-1', 'message' => '授权已过期，请先登录或注册'];
            }
        } else {
            return ['status' => '-1', 'message' => '缺少token值'];
        }
        return true;
    }

    /**
     * 登入与不登入都能操作
     * @param Request $request
     * @return string|array|bool
     */
    private function waitLogin(Request $request)
    {
        $header = $request->getHeaders();
        $token = $header['token'][0] ?? '';
        if (!empty($token)) {
            $user = UserRedis::getInstance()->user($token);
            if (!empty($user)) {
                $request->withAttribute('user', $user);
            }
        }
        return true;
    }


    /**
     * 基础中间件
     * @param Request $request
     * @return string|array|bool
     */
    private function apiMiniBase(Request $request)
    {
        return true;
    }
}